package org.maven.dependencybrowser.implementations;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.maven.dependencybrowser.assembler.Assembler;
import org.maven.dependencybrowser.assembler.AssemblerException;
import org.maven.dependencybrowser.controller.DependencyResolutionException;
import org.maven.dependencybrowser.model.Artifact;
import org.maven.dependencybrowser.model.ArtifactInitialisationException;
import org.maven.dependencybrowser.model.PomFile;
import org.maven.dependencybrowser.model.RootPom;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public abstract class Workspace
{

    public static final String ROLE = "org.maven.dependencybrowser.implementations.Workspace";
    /**
     * the root of the pom hierarachy. this will be an entity in the workspace
     */
    protected RootPom root;

    private Assembler assembler;

    private Repository repository;

    public abstract PomFile loadModulePom(String module);

    public abstract String getPath(String moduleName);

    public abstract void reloadPomFile() throws ArtifactInitialisationException;

    public abstract boolean canCreateSnapshot();

    public RootPom getRoot()
    {
        return this.root;
    }

    protected PomFile loadPom(BufferedReader pomFile)
    {
        try
        {
            MavenXpp3Reader pomReader = new MavenXpp3Reader();
            Model model = pomReader.read(new BufferedReader(pomFile));
            return new PomFile(assembler.assemble(model, repository), null, Artifact.DEFAULT_SCOPE);
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (IOException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (XmlPullParserException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (ArtifactInitialisationException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (AssemblerException e)
        {
            throw new DependencyResolutionException(e);
        }
    }

}
